<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of diadiemhoc_model
 *
 * @author Trung
 */
class M_canbo extends CI_Model {

    private $_name_table = 'tbl_canbo';
    private $_Ma_CBGV = 'Ma_CBGV';
    private $_HoTen_CB = 'HoTen_CB';
    private $_GioiTinh_CB = 'GioiTinh_CB';
    private $_NgaySinh_CB = 'NgaySinh_CB';
    private $_ChucVu = 'ChucVu';
    private $_Ma_DiaDiemCongTac_FK = 'Ma_DiaDiemCongTac_FK';
    private $_SoDienThoai = 'SoDienThoai';
    private $_Email = 'Email';

    public function __construct() {
        parent::__construct();
    }

    public function getID() {
        $this->db->select_max($this->_Ma_CBGV);
        $query = $this->db->get($this->_name_table);
        $data = $query->row_array();
        $id = $data->Ma_CBGV;

        if (strlen($id) == 0) {
            $id = 'CB0000';
        }
        $id++;

        return $id;
    }

    public function them($MaDiaDiem = '') {
        $id = $this->getID();
        $ten = $this->getID();
        $diachi = $this->getID();
        $sdt = '0' . rand(1, 100) . rand(300000, 900000);
        $email = $ten . '@ctsv.fithou';


        $val = array($this->_Ma_CBGV => $id,
            $this->_HoTen_CB => $ten,
            $this->_GioiTinh_CB => $diachi,
            $this->_NgaySinh_CB => $email,
            $this->_Ma_DiaDiemCongTac_FK => $MaDiaDiem,
            $this->_SoDienThoai => $sdt,
            $this->_Email => $email);

        $this->db->insert($this->_name_table, $val);
        return $val;
    }

    public function thems($n = 100) {
        $res[] = '';
        for ($i = 1; $i < $n; ++$i)
            $res[] = $this->them();
        return $res;
    }

    public function getAllDonduyet($T) {
        $this->db->order_by("NgayLap", "asc");
        if($T<100){
        $this->db->where('TrangThai', $T);}
        
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'FK_Ma_BieuMau=Ma_BieuMau');
        $query = $this->db->get('tbl_don_sv');
        return $query->result_array();
    }

    public function getAllSinhvien($ma_don_sv) {
        $this->db->where('Ma_Don_SV', $ma_don_sv);
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        $this->db->join('tbl_bieumau', 'FK_Ma_BieuMau=Ma_BieuMau');
        $query = $this->db->get('tbl_don_sv');
        return $query->result_array();
    }

    public function getUpdateDon($ma_don_sv) {
        $d = array(
            'TrangThai' => 1
        );
        $this->db->where('Ma_Don_SV', $ma_don_sv);
        $this->db->update('tbl_don_sv', $d);
//        $query=$this->db->get('tbl_don_sv');
//        return $query->result_array();
    }

    public function getUpdate2($ma_donSV) {
        $note = $_POST['notes'];
        $data = array(
            'TrangThai' => -1,
            'GhiChu' => $note
        );
        $this->db->where('Ma_Don_SV', $ma_donSV);
        $this->db->update('tbl_don_sv', $data);
    }

    public function getSvKTKL() {
        $this->db->join('tbl_ketqua', 'Ma_SV=FK_Ma_SV');
        $query = $this->db->get('tbl_sinh_vien');
        return $query->result_array();
    }

}

?>
